   function [pdf,logpdf]=invwish_pdf(W,v,S) 
%  function [pdf,logpdf]=invwish_pdf(W,v,S)
%
%  Compute the pdf and log pdf of an Inverted-Wishart 
%                       W ~ IW(v,S) 
%
%  Formula Taken from Bauwens, L, Lubrano, M. and J.F. Richard 
%  "Bayesian Inference in Dynamic Econometric Models"  
%  Also matches notation in  Gelman. A., et. al "Bayesian Data Analysis" 
%  (for the multivariate case) 
%
%  Inputs 
%  ------
%  W  (qxq)  positive definite random matrix 
%  v  (1x1)  degrees of freedom 
%  S  (qxq)  symmetric positive definite scale matrix 
%
% Output 
% ------
% pdf       probability density function (includes normalizing constant) 
% logpdf 
%
% For the scalar case this is the IG2 density s.t. 
%
% (1/W)~IG ( v/2,2/S)
% With expectation   E(W)= s/(v-2)            if v>2 
%      variance      V(W)=( E(W)^2) 2/(v-4)   if v>4
%
% Written by Alejandro Justiniano
% Changed the log of the det to mydet 8/2/2005 
q=size(W,1); 
vet=((v+1 -[1:q]))/2;
logk=zeros(4,1); 
logk(1)=v*q*log(2)/2; 
logk(2)=q*(q-1)*log(pi)/4; 
logk(3)=sum(gammaln(vet)); 
% logk(4)=(-v/2)*log(det(S));
logk(4)=(-v/2)*mydet(S);
logk=sum(logk);
c1=-trace(S/W)/2; 
% c2=-(v+q+1)*log(det(W))/2; 
c2=-0.5*(v+q+1)*mydet(W); 
logpdf = c1 +c2 - logk; 
pdf=exp(logpdf); 